package model;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;
import java.util.LinkedList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.Timer;

public class Sprite implements ActionListener {

	private List<ImageIcon> spriteList = new LinkedList<ImageIcon>();
	private Timer timer;
	private int i = 0;
	
	public Sprite(String path){
		File dir = new File(path);
		File[] files = dir.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				return (pathname.getName().endsWith(".png") || pathname.getName().endsWith(".jpg"));
			}
		});
		for(File file : files){
			spriteList.add(new ImageIcon(file.getAbsolutePath()));
		}
	}
	
	public Sprite(String path, int interval){
		this(path);
		timer = new Timer(interval, this);
		timer.start();
	}
	
	public Sprite(List<ImageIcon> sprite) {
		setSpriteList(sprite);
	}

	public void setSpriteList(List<ImageIcon> spriteList) {
		this.spriteList = spriteList;
	}

	public List<ImageIcon> getSpriteList() {
		return spriteList;
	}

	public void draw(int x, int y, Graphics g) {
		if(spriteList.size()>0){
			g.drawImage(spriteList.get(i).getImage(), x, y, null);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		i++;
		if(i>spriteList.size()-1){
			i=0;
		}
	}
	
}
